package org.starti.metadata.impl;

import org.starti.metadata.Metadata;
import org.starti.metadata.MetadataFactory;
import org.starti.metadata.MetadataProvider;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.List;

/**
 * @author <a href="mailto:claudiu.dumitrescu@gmail.com">Claudiu Dumitrescu</a>
 */
public class SimpleMetadataFactory implements MetadataFactory {

    private List<MetadataProvider> metadataProviders;


    public MappedMetadata createClass(Class observedClass) {
        MappedMetadata metadataClass = new MappedMetadata();
        for (MetadataProvider metadataProvider : metadataProviders) {
            if (metadataProvider.supports(observedClass)) {
                Object metadataInformations = metadataProvider.getClassMetadata(observedClass);
                metadataClass.addMetadata(metadataInformations);
            }
        }

        return metadataClass;
    }

    public Metadata createProperty(Method element) {
        MappedMetadata metadata = new MappedMetadata();
        for (MetadataProvider metadataProvider : metadataProviders) {
            Object metadataInformations = metadataProvider.getPropertyMetadata(element);
            metadata.addMetadata(metadataInformations);
        }
        return metadata;
    }

    public Metadata createProperty(Field element) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public Metadata createMethod(Method element) {
        MappedMetadata metadata = new MappedMetadata();
        for (MetadataProvider metadataProvider : metadataProviders) {
            Object metadataInformations = metadataProvider.getMethodMetadata(element);
            metadata.addMetadata(metadataInformations);
        }
        return metadata;
    }

    public Collection<MetadataProvider> getProviders() {
        return metadataProviders;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void setMetadataProviders(List<MetadataProvider> metadataProviders) {
        this.metadataProviders = metadataProviders;
    }
}
